﻿using UnityEngine;
using System.Collections;
using System.Timers;

public class Enemy : MonoBehaviour
{
    Timer timer = new Timer(1000);
    SpriteRenderer spriteRenderer;
    public Sprite sprite;
    public Sprite sprite1;

    public KeyCode start;
    public float health;

    public bool isAwaken = true;
    public int timeElapsed = 0;
    private bool _playerOneCanAttackPlayerTwo = false;
    private bool _playerTwoCanAttackPlayerOne = false;
    protected virtual void Start()
    {
        health = 100;

        spriteRenderer = GetComponent<SpriteRenderer>();
        if (spriteRenderer.sprite == null)
            spriteRenderer.sprite = sprite1;
    }

    protected virtual void Update()
    {
        if (timeElapsed >= 300)
        {
            ChangeSprite();
            this.rigidbody2D.WakeUp();
            isAwaken = this.rigidbody2D.IsAwake();
            this.health = 100;
            timeElapsed = 0;
        }

        if (this.health <= 0)
        {
            if (isAwaken)
            {
                ChangeSprite();
            }
            this.rigidbody2D.Sleep();
            isAwaken = this.rigidbody2D.IsAwake();
            timeElapsed += 1;
        }

        if (Input.GetKeyDown(KeyCode.H))
        {
            this.health -= 10;
        }

        if (this._playerOneCanAttackPlayerTwo)
        {
            var h = GameObject.FindGameObjectWithTag("Player2");
            if (h != null)
            {
                var j = h.GetComponent<Player>();

                if (j.GetComponent<Player>() != null)
                    j.GetComponent<Player>().health -= 5;
            }
        }
        if (this._playerTwoCanAttackPlayerOne)
        {
            var h = GameObject.FindGameObjectWithTag("Player1");
            if (h != null)
            {
                var j = h.GetComponent<Player>();

                if (j.GetComponent<Player>() != null)
                    j.GetComponent<Player>().health -= 5;
            }
        }

        if (this._playerOneCanAttackPlayerTwo && Input.GetKey(KeyCode.F1))
        {
            this.health -= 10;          
        }
        if (this._playerTwoCanAttackPlayerOne && Input.GetKey(KeyCode.KeypadEnter))
        {
            this.health -= 10;
            Debug.Log("hi");
        }
    }

    protected virtual void ChangeSprite()
    {
        if ((spriteRenderer.sprite == sprite))
        {
            spriteRenderer.sprite = sprite1;
        }
        else
        {
            spriteRenderer.sprite = sprite;
        }
    }

    protected virtual void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player2")
            this._playerOneCanAttackPlayerTwo = true;
        if (other.gameObject.tag == "Player1")
            this._playerTwoCanAttackPlayerOne = true;
    }

    protected virtual void OnTriggerExit2D(Collider2D other)
    {
        this._playerOneCanAttackPlayerTwo = false;
        this._playerTwoCanAttackPlayerOne = false;
    }
}
